package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constanttype;
import com.foreknow.service.ConstanttypeService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/constanttype")
public class ConstanttypeController {
    @Autowired
    private ConstanttypeService constanttypeService;



    @RequestMapping("findAll1")
    public String findAll1() throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        List<Constanttype> all = constanttypeService.findAll();
        hs.put("list",all);
        return objectMapper.writeValueAsString(hs);


    }
    @RequestMapping(value = "/listConstanttype")
    @ResponseBody
    public String findByPageByCondition(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        Constanttype constanttype =new Constanttype();
        if(para.get("id")!=null){ constanttype.setId((Integer) para.get("id"));}
        if(para.get("constanttypecode")!=null){ constanttype.setConstanttypecode((String)para.get("constanttypecode"));}
        if(para.get("constanttypename")!=null){ constanttype.setConstanttypename((String) para.get("constanttypename"));}
        if(para.get("delmark")!=null){constanttype.setDelmark((Integer) para.get("delmark"));}

        Integer pageNum = (Integer)para.get("pageNum");
        Integer pageSize =  (Integer)para.get("pageSize");

        PageHelper.startPage(pageNum,pageSize);
        List<Constanttype> users = constanttypeService.findAll();
        PageInfo<Constanttype> pageInfo = new PageInfo<Constanttype>(users);

        hs.put("meta",new Constance("msg",200));
        hs.put("info",pageInfo);

        return objectMapper.writeValueAsString(hs);

    }



}
